package com.zhuoyue.biz.core.command.cmd.common.fac;

import com.zhuoyue.biz.core.api.domain.DeviceAndGatewayDO;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;
import com.zhuoyue.biz.core.command.cmd.common.fac.base.ZhiNengKongTiaoMianBanConstant;
import com.zhuoyue.common.constant.ControlType;
import com.zhuoyue.common.utils.CmdUtils;

import java.util.Map;

/**
 * @Author zhanghui
 * @Date: 2021/07/05/14:30
 * @Description:
 */
public class ZhiNengKongTiaoMianBan0Fac0502 {
    /**
     * @param deviceAndGatewayDO
     * @param controlReq
     * @param way                1：空调面板、2：地暖面板、3：新风面板
     * @return
     */
    public static String control(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq, Integer way, Boolean isGroup) {
        // 子索引
        String subIndex = "";
        // 控制参数
        String param = "";
        String controlType = controlReq.getControlType();
        Map<String, Object> actions = controlReq.getActions();

        //获取subIndex和param
        switch (way) {
            //空调面板，有开、关、设置模式、风速、温度
            case 1:
                switch (controlType) {
                    case ControlType.TURN_ON_REQUEST:
                        subIndex = "17";
                        param = "01";
                        break;
                    case ControlType.TURN_OFF_REQUEST:
                        subIndex = "17";
                        param = "02";
                        break;
                    case ControlType.SET_MODE_REQUEST:
                        subIndex = "17";
                        String mode = actions.get("mode").toString();
                        param = ZhiNengKongTiaoMianBanConstant.AirConditionModeEnum.getValueByName(mode);
                        break;
                    case ControlType.SET_FAN_SPEED_REQUEST:
                        subIndex = "18";
                        String fanSpeed = actions.get("fanSpeed").toString();
                        param = ZhiNengKongTiaoMianBanConstant.AirConditionFanSpeedEnum.getValueByName(fanSpeed);
                        break;
                    case ControlType.SET_TEMPERATURE_REQUEST:
                        subIndex = "19";
                        float targetTemperature = Float.parseFloat(actions.get("targetTemperature").toString());
                        param = getTemperatureCmd(targetTemperature);
                        break;
                    default:
                        break;
                }
                break;
            //地暖面板，有开、关、设置温度
            case 2:
                switch (controlType) {
                    case ControlType.TURN_ON_REQUEST:
                        subIndex = "1B";
                        param = "01";
                        break;
                    case ControlType.TURN_OFF_REQUEST:
                        subIndex = "1B";
                        param = "02";
                        break;
                    case ControlType.SET_TEMPERATURE_REQUEST:
                        subIndex = "1C";
                        float targetTemperature = Float.parseFloat(actions.get("targetTemperature").toString());
                        param = getTemperatureCmd(targetTemperature);
                    default:
                        break;
                }
                break;
            //新风面板，有开、关、设置风速
            case 3:
                switch (controlType) {
                    case ControlType.TURN_ON_REQUEST:
                        subIndex = "1D";
                        param = "01";
                        break;
                    case ControlType.TURN_OFF_REQUEST:
                        subIndex = "1D";
                        param = "02";
                        break;
                    case ControlType.SET_FAN_SPEED_REQUEST:
                        subIndex = "1E";
                        String fanSpeed = actions.get("fanSpeed").toString();
                        param = ZhiNengKongTiaoMianBanConstant.FreshAirFanSpeed.getValueByName(fanSpeed);
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }

        return cmdDeal(deviceAndGatewayDO.getGatewayMac(), deviceAndGatewayDO.getZigbeeMac(), subIndex, param, isGroup);
    }

    /**
     * 处理空调面板的公共部分
     *
     * @param gatewayMac
     * @param deviceMac
     * @param subIndex   子索引
     * @param paramCmd   数据
     * @return
     */
    private static String cmdDeal(String gatewayMac, String deviceMac, String subIndex, String paramCmd, boolean isGroup) {
        String cmd1 = "02";
        String cmd2 = isGroup ? "02" : "01";
        String cmd5 = "0FAC";
        String cmd7 = CmdUtils.getDateLength(paramCmd);
        return cmd1 + cmd2 + gatewayMac + deviceMac + cmd5 + subIndex + cmd7 + paramCmd;
    }

    /**
     * 获取温度（16~30）
     *
     * @param temperature
     * @return
     */
    private static String getTemperatureCmd(float temperature) {
        temperature = Math.min(30, temperature);
        temperature = Math.max(16, temperature);
        int temperatureInt = (int) temperature;
        String temperatureIntString = temperatureInt + "";
        if (temperatureIntString.length() == 1) {
            temperatureIntString = "0" + temperatureIntString;
        }
        //取小数点后两位
        int temperaturePoint = ((int) (temperature * 100)) % 100;
        String temperaturePointString = temperaturePoint + "";
        if (temperaturePointString.length() == 1) {
            temperaturePointString = "0" + temperaturePointString;
        }

        return temperatureIntString + temperaturePointString;
    }
}
